package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.domain.Menu;
import com.itheima.domain.Role;
import com.itheima.domain.User;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.MenuMapper;
import com.itheima.mapper.RoleMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 菜单管理实现
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    //注入mapper
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;

    //分页查询
    public PageResult findPage(QueryPageBean queryPageBean,String username) {
        User user = userMapper.findByUsername(username);
        if (user == null){
            return null;
        }
        Integer userId = user.getId();
        Set<Role> roles = roleMapper.findByUserId(userId);
        //设置容器存入所需的数据
        List<Menu> result = new ArrayList<>();
        for (Role role : roles) {
            Integer roleId = role.getId();
            //通过角色Id获取对应的菜单
            List<Menu> menus = menuMapper.findByRoleId(roleId);

            //遍历菜单，获取等级为一的菜单
            for (Menu menu : menus) {
                //获取菜单级别
                Integer level = menu.getLevel();
                Integer menuId = menu.getId();//获取父级菜单id
                //判断菜单级别是否为1
                if(level == 1){
                    //创建子菜单容器并将子菜单放入
                    List<Menu> list = new ArrayList<>();
                    //再次遍历所有菜单，获取子菜单
                    for (Menu menu1 : menus) {
                        //获取该菜单的父Id
                        Integer parentMenuId = menu1.getParentMenuId();
                        //判断一级菜单id是否为该子菜单的父id
                        if (menuId == parentMenuId) {
                            //将该菜单放入容器
                            list.add(menu1);
                        }
                    }
                    //将子菜单容器赋值父菜单
                    menu.setChildren(list);
                    //将该具有子菜单的一级菜单放入容器
                    result.add(menu);
                }
            }
        }
//        Integer currentPage = queryPageBean.getCurrentPage();
//        Integer pageSize = queryPageBean.getPageSize();
//        String queryString = queryPageBean.getQueryString();
//        PageHelper.startPage(currentPage,pageSize);
//        Page<Menu> page =  menuMapper.selectByCondition(queryString);
        long total = result.size();
        return new PageResult(total, result);
    }

    //新增菜单项
    public void add(Menu menu) {
        menuMapper.add(menu);
    }

    //根据id查询菜单项信息
    public Menu findById(Integer id) {
        return menuMapper.findById(id);
    }

    //编辑菜单项
    public void edit(Menu menu) {
        menuMapper.edit(menu);
    }

    //根据id删除菜单项
    public void delete(Integer id) {
        menuMapper.deleteRoleIds(id);
        menuMapper.delete(id);
    }

    //查询所有
    public List<Menu> findAll() {
        return menuMapper.findAll();
    }

    //根据菜单名查询数据库是否该菜单已存在
    public Menu findByName(String name) {
        return menuMapper.findByName(name);
    }

    //根据菜单path查询数据库是否该菜单已存在
    public Menu findByPath(String path) {
        return menuMapper.findByPath(path);
    }

    //根据菜单id查询关联数据的数量
    public Integer findCountByMenuId(Integer menuId) {
        return menuMapper.findCountByMenuId(menuId);
    }

    //查询所有的菜单排除自己
    public List<Menu> findAllExOwn(Integer id) {
        return menuMapper.findAllExOwn(id);
    }
}
